package com.fofia.iotweb.web.controllers.admin;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.cache.UserCache;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.web.dbdomain.dto.MenuDto;
import com.fofia.iotweb.web.dbdomain.models.admin.SysMenu;
import com.fofia.iotweb.web.dbdomain.models.admin.SysRole;
import com.fofia.iotweb.web.dbdomain.services.admin.SysMenuService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.KeyValueInt64Dto;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.snack.ONode;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;

import java.util.List;


@Mapping("admin/sysmenu")
@Controller
public class SysMenuController extends BaseController {

    @Inject
    private RequestHelper requestHelper;
    @Inject
    private UserCache userCache;
    @Inject
    private SysMenuService menuService;

    @Get
    @Mapping("index")
    @AuthPermissions("admin.menu.view")
    public ModelAndView index(){

        return view();
    }

    @Get
    @Mapping("getMenuList")
    @AuthPermissions("admin.menu.view")
    public List<SysMenu> getMenuList(DataTableDto dto){

        List<SysMenu> data=menuService.getMenus();
        return data;
    }

    @Get
    @Mapping("form")
    @AuthPermissions("admin.menu.add;admin.menu.edit")
    public ModelAndView form(){
        String id= requestHelper.getString("id","0");
        List<KeyValueInt64Dto> menuSel=menuService.getMenuSelect();
        return view().put("id",id)
                .put("menuSel", menuSel);
    }

    @Get
    @Mapping("getModel")
    @AuthPermissions("admin.menu.add;admin.menu.edit")
    public ResultAdaptDto getModel(){
        long id=requestHelper.getInt64("id",0L);
        if(id<1){
            return JsonResult.failure("参数错误");
        }


        ResultAdaptDto ret=JsonResult.success();
        MenuDto model= menuService.getModel(id);
        ret.put("model",model);
        return  ret;
    }

    @Post
    @Mapping("saveData")
    @AuthPermissions("admin.menu.add;admin.menu.edit")
    public ResultAdaptDto saveData(SysMenu menuDto){
        String funcs=requestHelper.getString("funcs","");
//        StaticLog.info(funcs);
//        String json=ONode.serialize(menuDto);
//        StaticLog.info(json);

        menuService.saveMenu(menuDto,funcs);
        return  JsonResult.success("数据保存成功");
    }

    @Get
    @Mapping("removeAll")
    @AuthPermissions("admin.menu.delete")
    public ResultAdaptDto removeAll(){
        String ids=requestHelper.getString("ids","");
        if(StrUtil.isEmpty(ids)){
            return JsonResult.failure("参数错误");
        }
       menuService.removeAll(ids);
        return  JsonResult.success("数据删除成功");
    }

    @Post
    @Mapping("delfunc")
    @AuthPermissions("admin.menu.edit,admin.menu.delete")
    public ResultAdaptDto delfunc(){
        String id=requestHelper.getString("id","0");
        menuService.delfunc(Convert.toLong(id));
        return  JsonResult.success("数据删除成功");
    }
}
